﻿Imports System.Data.OleDb

Public Class details
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub btn_AddComment_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_AddComment.Click
        Dim sql As String = "INSERT INTO Comments (Author,Title,CommentBody,food_fkID)VALUES (@j1,@j2,@j3,@j4)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("JamaicanConnectionString").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

        cmd.Parameters.AddWithValue("@j1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@j2", tb_title.Text)
        cmd.Parameters.AddWithValue("@j3", tb_comment.Text)
        cmd.Parameters.AddWithValue("@j4", Request.QueryString("ID"))
        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()
        Response.Redirect("details.aspx?ID=" & Request.QueryString("ID"))

    End Sub

    Protected Sub btn_AddTags_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_AddTags.Click
        Dim sql As String = "INSERT INTO DishTags (Dish_fkID,Tag_fkId)VALUES (@j1,@j2)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("JamaicanConnectionString").ConnectionString)
        conn.Open()
        For Each cb As ListItem In cb_tags.Items
            If cb.Selected = True Then
                'add this to the database
                Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
                cmd.Parameters.AddWithValue("@j1", Request.QueryString("ID"))
                cmd.Parameters.AddWithValue("@j2", cb.Value)
                cmd.ExecuteNonQuery()



            End If
        Next


    End Sub

    Protected Sub btn_NewTag_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_NewTag.Click
        Dim sql As String = "INSERT INTO Tags (TagName)VALUES (@j1)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("JamaicanConnectionString").ConnectionString)
        conn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@j1", tb_NewTag.Text)
        cmd.ExecuteNonQuery()

        Dim cmd2 As OleDbCommand = New OleDbCommand("SELECT TOP 1 ID FROM Tags ORDER BY ID Desc", conn)
        Dim dr As OleDbDataReader = cmd2.ExecuteReader()
        dr.Read()
        Dim highestID = dr(0)

        Dim relate As String = "INSERT INTO DishTags (Dish_fkID,Tag_fkID)VALUES(@j1,@j2)"
        Dim cmd3 As OleDbCommand = New OleDbCommand(relate, conn)
        cmd3.Parameters.AddWithValue("@j1", Request.QueryString("ID"))
        cmd3.Parameters.AddWithValue("@j2", highestID)
        cmd3.ExecuteNonQuery()




    End Sub

    
End Class